<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/3/27
 * Time: 10:02
 * Mail:87658989@qq.com
 */

namespace app\base\service;


class GeTui
{
    public $appid;
    public $appkey;
    public $mkey;
    public $host;
    public function __construct($appid='whNRBatRsT6Uc36WwihRd8',$appkey='D3RHZhMqh16qk3WJc0IXO3',$mkey='8Pud63cYng9HiNtsQhp6DA')
    {
        $this->appid = $appid;
        $this->appkey= $appkey;
        $this->host  = 'http://sdk.open.api.igexin.com/apiex.htm';
        $this->mkey  = $mkey;
        //aZ5oGC7WaZ70fTZPpvgPO2 appscre
    }

    public function setKey($appid,$appkey, $mkey) {
        $this->appid       = $appid;
        $this->mkey        = $mkey;
        $this->appkey= $appkey;
    }
    public function pushMessage($cid='f4c6ebfe60d004eaef7fc891333da481',$msg='',$notice='',$expire=86400,$title='晋江出行'){
        import('getui.IGt', EXTEND_PATH,'.Push.php');
        $igt      = new \IGeTui($this->host,$this->appkey,$this->mkey);
        // $rep = $igt->getClientIdStatus($this->appid,$cid);//CID状态查询
        $template =  new \IGtNotificationTemplate();
        $template->set_appId($this->appid);//应用appid
        $template->set_appkey($this->appkey);//应用appkey
//         $template->set_transmissionType(1);//透传消息类型
//         $template->set_transmissionContent('333');//透传内容
        $template->set_title($title);//通知栏标题
        $template->set_text($notice);//通知栏内容
        // $template->set_logo("http://wwww.igetui.com/logo.png");//通知栏logo
        $template->set_isRing(true);//是否响铃
        $template->set_isVibrate(true);//是否震动
        $template->set_isClearable(true);//通知栏是否可清除
        $message = new \IGtSingleMessage();
        $message->set_isOffline(true);//是否离线true
        $message->set_offlineExpireTime($expire);//离线时间
        $message->set_data($template);//设置推送消息类型
        // $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
        $template1 =  new \IGtTransmissionTemplate();
        $template1->set_appId($this->appid);//应用appid
        $template1->set_appkey($this->appkey);//应用appkey
        $template1->set_transmissionType(1);//透传消息类型
        $template1->set_transmissionContent($msg);//透传内容
        // $template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
        $apn                   = new \IGtAPNPayload();
        $alertmsg              = new \DictionaryAlertMsg();
        $alertmsg->body        = $notice;
        $alertmsg->actionLocKey= "ActionLockey";
        $alertmsg->locKey      = $notice;
        $alertmsg->locArgs     = ["locargs"];
        $alertmsg->launchImage = "launchimage";
        $alertmsg->title       = $title;
        $alertmsg->titleLocKey = $title;
        $alertmsg->titleLocArgs= ["TitleLocArg"];
        $apn->alertMsg         = $alertmsg;
        $apn->badge            = 1;
        $apn->sound            = "";
        $apn->add_customMsg("payload","payload");
        $apn->contentAvailable = 1;
        $apn->category         = "ACTIONABLE";
        $template1->set_apnInfo($apn);
        $message1              = new \IGtSingleMessage();
        $message1->set_isOffline(true);//是否离线
        $message1->set_offlineExpireTime(60);//离线时间
        $message1->set_data($template1);//设置推送消息类型
        $target                = new \IGtTarget();
        $target->set_appId($this->appid);
        $ret = '';
        $ret1= '';
        if(is_array($cid)){
            foreach ($cid as $id){
                $target->set_clientId($id);
                !empty($notice) && $ret = $igt->pushMessageToSingle($message, $target);//通知透传
                !empty($msg) && $ret1 = $igt->pushMessageToSingle($message1, $target);//单透传
            }
        }else{
            $target->set_clientId($cid);
            !empty($notice) && $ret = $igt->pushMessageToSingle($message, $target);//通知透传
            !empty($msg) && $ret1 = $igt->pushMessageToSingle($message1, $target);//单透传
        }
        return $ret.$ret1;
    }

    public function pushNotice($cid,$notice='',$title=''){
        import('getui.IGt', EXTEND_PATH,'.Push.php');
        $igt      = new \IGeTui($this->host,$this->appkey,$this->mkey);
        // $rep = $igt->getClientIdStatus($this->appid,$cid);//CID状态查询
        $template =  new \IGtNotificationTemplate();
        $template->set_appId($this->appid);//应用appid
        $template->set_appkey($this->appkey);//应用appkey
        // $template->set_transmissionType(1);//透传消息类型
        // $template->set_transmissionContent('333');//透传内容
        $template->set_title($title);//通知栏标题
        $template->set_text($notice);//通知栏内容
        // $template->set_logo("http://wwww.igetui.com/logo.png");//通知栏logo
        $template->set_isRing(true);//是否响铃
        $template->set_isVibrate(true);//是否震动
        $template->set_isClearable(true);//通知栏是否可清除
        $message = new \IGtSingleMessage();
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(24*60*60);//离线时间
        $message->set_data($template);//设置推送消息类型
        // $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
        $template1 =  new \IGtTransmissionTemplate();
        $template1->set_appId($this->appid);//应用appid
        $template1->set_appkey($this->appkey);//应用appkey
        // $template1->set_transmissionType(1);//透传消息类型
        // $template1->set_transmissionContent($msg);//透传内容
        // $template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
        $apn                   = new \IGtAPNPayload();
        $alertmsg              = new \DictionaryAlertMsg();
        $alertmsg->body        = $notice;
        $alertmsg->actionLocKey= "ActionLockey";
        $alertmsg->locKey      = "LocKey";
        $alertmsg->locArgs     = ["locargs"];
        $alertmsg->launchImage = "launchimage";
        $alertmsg->title       = $title;
        $alertmsg->titleLocKey = $title;
        $alertmsg->titleLocArgs= ["TitleLocArg"];
        $apn->alertMsg         = $alertmsg;
        $apn->badge            = 7;
        $apn->sound            = "";
        $apn->add_customMsg("payload","payload");
        $apn->contentAvailable = 1;
        $apn->category         = "ACTIONABLE";
        $template1->set_apnInfo($apn);
        $message1              = new \IGtSingleMessage();
        $message1->set_isOffline(true);//是否离线
        $message1->set_offlineExpireTime(0);//离线时间
        $message1->set_data($template1);//设置推送消息类型
        $target                = new \IGtTarget();
        $target->set_appId($this->appid);
        $ret = '';
        $ret1= '';
        if(is_array($cid)){
            foreach ($cid as $id){
                $target->set_clientId($id);
                !empty($notice) && $ret = $igt->pushMessageToSingle($message, $target);//通知透传
                !empty($notice) && $ret1 = $igt->pushMessageToSingle($message1, $target);//单透传
            }
        }else{
            $target->set_clientId($cid);
            !empty($notice) && $ret = $igt->pushMessageToSingle($message, $target);//通知透传
            !empty($notice) && $ret1 = $igt->pushMessageToSingle($message1, $target);//单透传
        }
        return $ret.$ret1;
    }
}